{% extends "base/base.html" %}

{% block title %}用户注册 - 旅游舆情监测{% endblock %}

{% block content %}
    <style>
        body, html {
            height: 100%;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .register-page {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #1e88e5, #0d47a1);
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .register-form {
            background: white;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
            width: 90%;
            max-width: 450px;
            animation: fadeInUp 0.6s ease;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .form-title {
            text-align: center;
            color: #1e88e5;
            margin-bottom: 30px;
            font-weight: bold;
            font-size: 28px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-control {
            border: 2px solid #e9ecef;
            border-radius: 8px;
            padding: 12px 15px;
            font-size: 14px;
            transition: all 0.3s ease;
            width: 100%;
            box-sizing: border-box;
        }

        .form-control:focus {
            border-color: #1e88e5;
            box-shadow: 0 0 0 0.2rem rgba(30, 136, 229, 0.25);
            outline: none;
        }

        .form-control.error {
            border-color: #dc3545;
        }

        .form-label {
            font-weight: 600;
            color: #333;
            margin-bottom: 8px;
            display: block;
        }

        .btn-register {
            background: linear-gradient(135deg, #1e88e5, #0d47a1);
            border: none;
            padding: 12px;
            font-weight: 600;
            font-size: 16px;
            transition: all 0.3s ease;
            border-radius: 8px;
            color: white;
            width: 100%;
            cursor: pointer;
        }

        .btn-register:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(30, 136, 229, 0.4);
        }

        .login-link {
            border: 2px solid #1e88e5;
            color: #1e88e5;
            text-decoration: none;
            transition: all 0.3s ease;
            border-radius: 8px;
            font-weight: 600;
            padding: 12px;
            display: block;
            text-align: center;
            margin-top: 15px;
        }

        .login-link:hover {
            background: #1e88e5;
            color: white;
            text-decoration: none;
            transform: translateY(-1px);
        }

        .system-info {
            text-align: center;
            margin-bottom: 25px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #1e88e5;
        }

        .system-info h6 {
            color: #1e88e5;
            margin-bottom: 5px;
        }

        .system-info p {
            color: #666;
            font-size: 12px;
            margin: 0;
        }

        /* 验证码样式 */
        .captcha-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .captcha-image {
            border: 1px solid #ddd;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
            height: 40px;
        }

        .captcha-image:hover {
            opacity: 0.8;
            transform: scale(1.05);
        }

        .captcha-refresh {
            color: #1e88e5;
            cursor: pointer;
            font-size: 14px;
            white-space: nowrap;
        }

        .captcha-refresh:hover {
            text-decoration: underline;
        }

        .error-message {
            color: #dc3545;
            font-size: 14px;
            margin-top: 5px;
            display: block;
        }

        .alert {
            padding: 12px 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .alert-danger {
            background-color: #f8d7da;
            border-color: #f5c6cb;
            color: #721c24;
        }

        .password-requirements {
            background: #f8f9fa;
            padding: 10px 15px;
            border-radius: 6px;
            font-size: 12px;
            color: #666;
            margin-top: 10px;
            border-left: 3px solid #1e88e5;
        }

        .password-requirements ul {
            margin: 5px 0;
            padding-left: 20px;
        }

        .field-errors {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            border-radius: 6px;
            padding: 10px 15px;
            margin-bottom: 20px;
        }

        .field-errors ul {
            margin: 0;
            padding-left: 20px;
        }
    </style>

    <div class="register-page">
        <div class="register-form">
            <h2 class="form-title">
                <i class="fa fa-user-plus"></i> 用户注册
            </h2>

            <!-- 系统信息 -->
            <div class="system-info">
                <h6><i class="fa fa-map-marker"></i> 旅游舆情监测系统</h6>
                <p>实时监测景点舆情，分析用户情感倾向</p>
            </div>

            <!-- 调试信息：显示所有表单数据 -->
            {% if form.errors %}
            <div class="field-errors">
                <strong><i class="fa fa-exclamation-triangle"></i> 表单验证失败：</strong>
                <ul>
                    {% for field, errors in form.errors.items %}
                        {% for error in errors %}
                            <li><strong>{{ field }}:</strong> {{ error }}</li>
                        {% endfor %}
                    {% endfor %}
                </ul>
            </div>
            {% endif %}

            <form method="post" id="registerForm">
                {% csrf_token %}

                <!-- 用户名字段（隐藏或可见） -->
                <div class="form-group" style="display: none;">
                    <label for="id_username" class="form-label">用户名</label>
                    <input type="text" name="username" id="id_username" class="form-control">
                </div>

                <!-- 邮箱字段 -->
                <div class="form-group">
                    <label for="id_email" class="form-label">
                        <i class="fa fa-envelope"></i> 邮箱地址
                    </label>
                    <input type="email" name="email" id="id_email" class="form-control {% if form.email.errors %}error{% endif %}"
                           value="{{ form.email.value|default:'' }}" placeholder="请输入您的邮箱地址" required>
                    {% if form.email.errors %}
                        <div class="error-message">
                            {% for error in form.email.errors %}
                                <i class="fa fa-exclamation-circle"></i> {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>

                <!-- 密码字段 -->
                <div class="form-group">
                    <label for="id_password1" class="form-label">
                        <i class="fa fa-lock"></i> 密码
                    </label>
                    <input type="password" name="password1" id="id_password1" class="form-control {% if form.password1.errors %}error{% endif %}"
                           placeholder="请设置密码" required>
                    {% if form.password1.errors %}
                        <div class="error-message">
                            {% for error in form.password1.errors %}
                                <i class="fa fa-exclamation-circle"></i> {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                    <div class="password-requirements">
                        <strong>密码要求：</strong>
                        <ul>
                            <li>至少8个字符</li>
                            <li>包含字母和数字</li>
                        </ul>
                    </div>
                </div>

                <!-- 确认密码字段 -->
                <div class="form-group">
                    <label for="id_password2" class="form-label">
                        <i class="fa fa-lock"></i> 确认密码
                    </label>
                    <input type="password" name="password2" id="id_password2" class="form-control {% if form.password2.errors %}error{% endif %}"
                           placeholder="请再次输入密码" required>
                    {% if form.password2.errors %}
                        <div class="error-message">
                            {% for error in form.password2.errors %}
                                <i class="fa fa-exclamation-circle"></i> {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>

                <!-- 验证码字段 -->
                <div class="form-group">
                    <label class="form-label">
                        <i class="fa fa-shield"></i> 验证码
                    </label>
                    <div class="captcha-group">
                        {{ form.captcha }}
                        <span class="captcha-refresh" onclick="refreshCaptcha()">
                            <i class="fa fa-refresh"></i> 换一张
                        </span>
                    </div>
                    {% if form.captcha.errors %}
                        <div class="error-message">
                            {% for error in form.captcha.errors %}
                                <i class="fa fa-exclamation-circle"></i> {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>

                <!-- 显示非字段错误 -->
                {% if form.non_field_errors %}
                    <div class="alert alert-danger">
                        <i class="fa fa-exclamation-triangle"></i>
                        {% for error in form.non_field_errors %}
                            {{ error }}
                        {% endfor %}
                    </div>
                {% endif %}

                <button type="submit" class="btn-register">
                    <i class="fa fa-user-plus"></i> 立即注册
                </button>

                <a href="{% url 'users:log' %}" class="login-link">
                    <i class="fa fa-sign-in"></i> 已有账号？立即登录
                </a>
            </form>
        </div>
    </div>
{% endblock %}

{% block js %}
<script>
function refreshCaptcha() {
    fetch("/captcha/refresh/")
        .then(response => response.json())
        .then(result => {
            document.querySelector('.captcha').src = result.image_url;
            document.getElementById('id_captcha_0').value = result.key;
        })
        .catch(error => {
            console.error('刷新验证码失败:', error);
            alert('验证码刷新失败，请重试');
        });
}

document.addEventListener('DOMContentLoaded', function() {
    // 自动设置username为email的值
    const emailInput = document.getElementById('id_email');
    const usernameInput = document.getElementById('id_username');

    if (emailInput && usernameInput) {
        emailInput.addEventListener('input', function() {
            usernameInput.value = this.value;
        });
    }

    // 点击验证码刷新
    const captchaImage = document.querySelector('.captcha');
    if (captchaImage) {
        captchaImage.addEventListener('click', refreshCaptcha);
    }

    // 表单验证
    const registerForm = document.getElementById('registerForm');
    if (registerForm) {
        registerForm.addEventListener('submit', function(e) {
            const password1 = document.getElementById('id_password1');
            const password2 = document.getElementById('id_password2');

            // 确保username有值
            if (usernameInput && !usernameInput.value && emailInput) {
                usernameInput.value = emailInput.value;
            }

            if (password1 && password2) {
                if (password1.value !== password2.value) {
                    alert('两次输入的密码不一致！');
                    e.preventDefault();
                    return false;
                }

                if (password1.value.length < 8) {
                    alert('密码长度至少8位！');
                    e.preventDefault();
                    return false;
                }
            }

            // 显示加载状态
            const submitBtn = this.querySelector('.btn-register');
            if (submitBtn) {
                submitBtn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> 注册中...';
                submitBtn.disabled = true;
            }
        });
    }
});
</script>
{% endblock %}